﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SGD;

namespace ParticleEditor
{
    class Particle
    {
        private float x, y, velX, velY, Scale, LifeTime, StartTime;

        public float _X
        {
            get { return x; }
            set { x = value; }
        }

        public float _Y
        {
            get { return y; }
            set { y = value; }
        }

        public float _velX
        {
            get { return velX; }
            set { velX = value; }
        }

        public float _velY
        {
            get { return velY; }
            set { velY = value; }
        }

        public float _scale
        {
            get { return Scale; }
            set { Scale = value; }
        }

        public float _LifeTime
        {
            get { return LifeTime; }
            set { LifeTime = value; }
        }

        public float _StartTime
        {
            get {return StartTime; }
            set { StartTime = value; }
        }

        private float A, R, G, B;

        public float A1
        {
            get { return A; }
            set { A = value; }
        }

        public float R1
        {
            get { return R; }
            set { R = value; }
        }

        public float G1
        {
            get { return G; }
            set { G = value; }
        }

        public float B1
        {
            get { return B; }
            set { B = value; }
        }

        
        private ParticleEmitter Emitter;

        public void SetEmitter(ParticleEmitter emitter)
        {
            Emitter = emitter;
        }

        public Particle(float _X = 0, float _Y = 0, float _VelX = 0, float _VelY = 0, float _Scale = 1, float _Lifetime = 1, float _StartTime = 0)
        {
            x = _X;
            y = _Y;

            velX = _VelX;
            velY = _velY;

            Scale = _Scale;

            LifeTime = _Lifetime;

            StartTime = _StartTime;

            A = 255;
            R = 255;
            B = 255;
            G = 255;

        }

        public void Update(float elapsedTime, bool V_Timebased = false, bool V_CirlceBased = false)
        {
            if (StartTime <= 0)
            {
                LifeTime -= elapsedTime;


                if (V_Timebased)
                {
                    velX += ((Emitter.velX_s - Emitter.velX_f) / Emitter.ParticleLifetime) * elapsedTime;
                    velY += ((Emitter.velY_s - Emitter.velY_f) / Emitter.ParticleLifetime) * elapsedTime;
                }
                Scale -= ((Emitter.scale_s - Emitter.scale_f) / Emitter.ParticleLifetime) * elapsedTime;

                A -= ((Emitter.Alpha_s1 - Emitter.Alpha_f1) / Emitter.ParticleLifetime) * elapsedTime;
                R -= ((Emitter.Start1.R - Emitter.Finish1.R) / Emitter.ParticleLifetime) * elapsedTime;
                G -= ((Emitter.Start1.G - Emitter.Finish1.G) / Emitter.ParticleLifetime) * elapsedTime;
                B -= ((Emitter.Start1.B - Emitter.Finish1.B) / Emitter.ParticleLifetime) * elapsedTime;

                if (A > 255)
                    A = 255;
                else if (A < 0)
                    A = 0;

                if (R > 255)
                    R = 255;
                else if (R < 0)
                    R = 0;

                if (G > 255)
                    G = 255;
                else if (G < 0)
                    G = 0;

                if (B > 255)
                    B = 255;
                else if (B < 0)
                    B = 0;

                x = x + (velX * elapsedTime);
                y = y + (velY * elapsedTime);

            }
            else
                StartTime -= elapsedTime;
        }

        public void Render(int imageID, Point center)
        {
            ManagedTextureManager.Instance.Draw(imageID, ((int)x + center.X) , (int)y + center.Y, Scale, Scale, Rectangle.Empty, 0, 0, 0, Color.FromArgb((int)A, (int)R, (int)G, (int)B).ToArgb());
        }

    }
}
